package com.jxust.controller;

import com.jxust.pojo.Result;
import com.jxust.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController

public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        log.info("文件上传:{}",file.getOriginalFilename());
        //将文件交给OSS存储管理
        try {
            String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
            log.info("文件上传成功:{}",url);
            return Result.success(url);
        } catch (Exception e) {
            log.error("文件上传失败:{}",e.getMessage());
            return Result.error("上传失败");
        }
    }
}
